// $Id: VariablesView.java 76 2011-12-30 21:40:44Z huijzer@gmail.com $
package lime.workflow.view;

import java.util.Collections;
import java.util.List;

import lime.model.Variable;
import lime.model.Workitem;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableColumn;

public class VariablesView extends AbstractWorkitemListenerView {

	public static final String ID = "lime.workflow.view.variables";

	// Widgets
	private TableViewer viewer;

	@Override
	public void createPartControl(Composite parent) {

		viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.BORDER);
		viewer.getTable().setHeaderVisible(true);
		viewer.getTable().setLinesVisible(true);

		TableViewerColumn nameColumn = new TableViewerColumn(viewer, SWT.NONE);
		nameColumn.getColumn().setText("Name");
		TableViewerColumn valueColumn = new TableViewerColumn(viewer, SWT.NONE);
		valueColumn.getColumn().setText("Value");

		viewer.setContentProvider(new VariableContentProvider());
		viewer.setLabelProvider(new VariableLabelProvider());

		// Make sure the columns take up as much space as needed, but no more.
		for (TableColumn column : viewer.getTable().getColumns()) {
			column.pack();
		}
	}

	@Override
	public void setFocus() {
		viewer.getTable().setFocus();
	}

	protected void update(final Workitem workitem) {

		final Thread myThread = new Thread("MyThread") {
			public void run() {
				String workflowId = workitem.getWorkflowId();
				final List<Variable> variables = getWorkflowService().getWorkflowVariables(workflowId);
				Collections.sort(variables);
				getSite().getShell().getDisplay().asyncExec(new Runnable() {
					public void run() {
						setContentDescription(workitem.getProcessName() + " :: " + workitem.getActivityName());
						viewer.setInput(variables);
						// Make sure the columns take up as much space as
						// needed, but no more.
						for (TableColumn column : viewer.getTable().getColumns()) {
							column.pack();
						}
						viewer.refresh();
					}
				});
			}
		};
		myThread.start();
	}

	private class VariableContentProvider implements IStructuredContentProvider {

		@SuppressWarnings("unchecked")
		@Override
		public Object[] getElements(Object element) {
			List<Variable> variables = (List<Variable>) element;
			return variables.toArray();
		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub

		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// TODO Auto-generated method stub

		}
	}

	private class VariableLabelProvider implements ITableLabelProvider {

		@Override
		public void addListener(ILabelProviderListener listener) {
			// TODO Auto-generated method stub

		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub

		}

		@Override
		public boolean isLabelProperty(Object element, String property) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void removeListener(ILabelProviderListener listener) {
			// TODO Auto-generated method stub

		}

		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			Variable variable = (Variable) element;
			String text = "";

			switch (columnIndex) {
				case 0:
					text = variable.getName();
					break;
				case 1:
					text = StringUtils.join(variable.getValues(), ", ");
					break;
				default:
					break;
			}

			return text;
		}
	}

}